<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="com.xiaoma.tpo.domain.User" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  User currentUser = (User)session.getAttribute("CURRENT_USER");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>广告</title>
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/easyui.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/icon.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/neweasyui/themes/gray/panel.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/neweasyui/themes/gray/window.css">
  <script type="text/javascript" src="<%=path %>/script/1.11.1.js"></script>
  <script type="text/javascript" src="<%=path %>/script/jquery.easyui.min.js"></script>
  <script type="text/javascript" src="<%=path %>/script/easyui-lang-zh_CN.js"></script>
  <script type="text/javascript" src="<%=path %>/script/dateFormat.js"></script>
  <script type="text/javascript" src="<%=path %>/script/datagrid-bufferview.js"></script>
  <script type="text/javascript" src="<%=path %>/script/common.js"></script>
  <script type="text/javascript" src="<%=path %>/script/ckeditor/ckeditor.js"></script>
  <style type="text/css">
    #fm{
      margin:0;
      padding:10px 30px;
    }
    .ftitle{
      font-size:14px;
      font-weight:bold;
      padding:5px 0;
      margin-bottom:10px;
      border-bottom:1px solid #ccc;
    }
    .fitem{
      margin-bottom:17px;
    }
    .fitem label{
      display:inline-block;
      width:80px;
    }
    .fitem input{
      width:230px;
    }
  </style>
</head>
<body>
<table id="tt" title="列表" style="width:auto;height:500px">
</table>
<!-----------------------------------------------------------------按钮------------------------------------------------------------------->
<div id="toolbar" style="padding:5px;height:auto">
  <div>
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-add" onclick="addTopic()">添加</a>&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-edit" onclick="editTopic()">修改</a>&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-remove" onclick="deleteTopic()">删除</a>&nbsp;&nbsp;

  </div>
</div>
<!-------------------------------------------------------------添加/编辑框 开始----------------------------------------------------------------------->
<!-- 添加/编辑框 开始-->
<div id="addTopic" class="easyui-dialog"
     style="width: 1600px; height: 700px; padding: 10px 20px" closed="true"
     buttons="#dlg-buttons">
  <div class="ftitle">帖子信息</div>
  <form id="topic_form" method="post"  enctype="multipart/form-data">
    <input type="hidden" name="id" id="id" />

    <div class="fitem">
      <label>标题:</label>
      <input  class="easyui-textbox" name="title" id="title" value="" />
    </div>
    <div class="fitem">
      <label>图标:</label>
      <input  type="file" name="imgs" id="imgs" value="" />
    </div>
    <div class="fitem">
      <label>类型:</label>
        <select id="type" name="type" class="easyui-combobox">
		    <option value="1" selected="selected">机经</option>
		    <option value="2">口语</option>
		    <option value="3">写作</option>
		    <option value="4">阅读</option>
		    <option value="5">听力</option>
		</select>
      
    </div>
    <%if(currentUser.getRole().intValue() == 3) {%>
    <div class="fitem">
      <label>老师:</label>
      <input id="teacherId" name="teacherId" value="">
    </div>
    <%}%>
    <div class="fitem">
      <label>内容:</label>
      <textarea  rows="12" cols="80" style="width: 80%" id="content" name="content" ></textarea>
      <script type="text/javascript">CKEDITOR.replace('content');</script>
    </div>
    
  </form>
  <div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6"  iconCls="icon-ok" onclick="saveOrUpdateAd()" style="width: 90px">Save</a> 
    <a href="javascript:void(0)" class="easyui-linkbutton"  iconCls="icon-cancel" onclick="closeDialog()" style="width: 90px">Cancel</a>
  </div>
</div>
<!------------------------------------------------------------------------------------------------------------------------------------>

<!-- 添加/编辑框结束-->


</body>
<script type="text/javascript">
  $.ajaxSetup({cache:false});
  $(function(){
    $('#tt').datagrid({
      url:'<%=basePath %>sys/forum/list',
      method:'get',
      singleSelect:true,toolbar:'#toolbar',remoteSort:false,pageSize:10,nowrap:false,
      fitColumns:true,pagination:true,rownumbers:true,
      columns:[[

                {field:'id',title:'ID',sortable:false,width:15,hidden:true},

                {field:'userName',title:'User',sortable:false,width:80},

                {field:'title',title:'Title',sortable:false,width:80},

                {field:'content',title:'Content',sortable:false,width:80,hidden:true},

                {field:'imgs',title:'Imgs',sortable:false,width:80},
                
                {field:'type',title:'Type',sortable:false,width:80,
                	formatter: function(value,row,index){
                		var str = "";
                		if (row.type == 1) {
                			str = "机经";
                		} else if (row.type == 2) {
                			str = "口语";
                		} else if (row.type == 3) {
                			str = "写作";
                		} else if (row.type == 4) {
                			str = "阅读";
                		} else if (row.type == 5) {
                			str = "听力";
                		}
                		return str;
                	}}

              ]],
    
    });
  });
//------------------------------------------------------------------------------------------
  //关闭弹出框
  function closeDialog(){
    $('#addTopic').dialog('close') ;
  }

  //添加
  function addTopic(){
    $('#addTopic').dialog('open').dialog('setTitle','发贴');
    $('#topic_form').form('clear');
    $('#type').combobox('setValue',"机经");
    $('#type').combobox('select',1);
    CKEDITOR.instances.content.setData();
    $("#dlg-buttons").show();
    if ($('#teacherId').length > 0) {
	    $('#teacherId').combobox({
	        url:'<%=basePath%>sys/teacher/list',
	        valueField:'id',
	        textField:'name'
	    });
    }
    
    url = '<%=basePath%>sys/forum/save';
  }
  //修改
  function editTopic(){
    url = '<%=basePath%>sys/forum/edit';
    var row = $('#tt').datagrid('getSelected');
     if (row){
      $("#id").val(row.id);
      $("#title").val(row.title);
      $("#type").val(row.type);
      CKEDITOR.instances.content.setData(row.content);
      $('#addTopic').dialog('open').dialog('setTitle','编辑帖子');
      if ($('#teacherId').length > 0){
    	  $('#teacherId').combobox({
              url:'<%=basePath%>sys/teacher/list',
              valueField:'id',
              textField:'name'
          });
      }
      if (row.userName == '<%=currentUser.getName() %>' || <%= currentUser.getRole()%> == 3) {
    	  $("#dlg-buttons").show();
      } else {
          $("#dlg-buttons").hide();
      }
      $('#topic_form').form('load',row);
    } 
  }


  function deleteTopic(){

    var row = $('#tt').datagrid('getSelected');
    if (row){
    	if (row.userName == '<%=currentUser.getName() %>' || <%= currentUser.getRole()%> == 3) {
	      $.messager.confirm('提示','确定要删除这个帖子吗？',function(r){
	        if (r){
	        	var id = row.id;
	          $.post('<%=basePath%>sys/forum/remove/'+ id,function(result){
	            $('#tt').datagrid('reload');    // reload the data
	          },'json');
	        }
	      });
        } else {
            alert("不能删除其他人的帖子");
        }
    }
  }
//---------------------------------------------------------------------------------------------------------------------------
  //添加或修改广告
  function saveOrUpdateAd(){
	  var input = document.getElementById("imgs");
	  if (input.value) {
		  var imgs = input.files[0];
		  if (imgs.size > 100000) {
			  alert("图片不能大于100K");
			  return;
		  }
		  var fname = imgs.name;
		  var extStart=fname.lastIndexOf(".");
		  var ext=fname.substring(extStart,fname.length).toUpperCase();
	      if(ext!=".PNG"&&ext!=".JPG"){
	          alert("只支持PNG,JPG格式");
	          return;
	      }
	  }
	 
    $('#topic_form').form('submit', {
      url : url,
      onSubmit : function() {
        return $(this).form('validate');
      },
      success : function(result) {
        var data = eval('(' + result + ')');      
        if (data.status == 0) {
          $.messager.show({
            title : 'Error',
            msg : data.message
          });
        }else {
          $('#addTopic').dialog('close'); // close the dialog
          $('#tt').datagrid('reload'); // reload the data
        } 
      }
    });
  }


</script>
</html>
